package com.mrd.shiptask_shipowner.activity;

import com.mrd.shiptask_shipowner.R;
import com.mrd.shiptask_shipowner.constants.HttpConstant;
import com.mrd.shiptask_shipowner.serviecs.initdatadao.InitDatasDaoImpl;
import com.mrd.shiptask_shipowner.unit_tools.FunctionOfActivity;
import com.mrd.shiptask_shipowner.unit_tools.SysApplication;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class ConfirmOrderActivity extends Activity {

	/** 全局的LayoutInflater对象，已经完成初始化. */
	public LayoutInflater mInflater;
	private final ConfirmOrderActivity THIS = ConfirmOrderActivity.this;
	private Button seeLoadPicBt, seeDisPicBt, confirmResultBt, backToBeforeBt6;
	private TextView loadingPortTv, DischargePortTv, loadingAmountTv,
			DischargeAmountTv, unitTv, lastUnAmountTv;
	private EditText infoAmountEt;
	private LinearLayout LL;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.confirm_order_activity_manifest);
		initView();
		SysApplication.getInstance().addActivity(this);
	}

	private void initView() {
		TextView topTitleTv = (TextView) findViewById(R.id.topTitleTv);
		topTitleTv.setText("确认签单");
		backToBeforeBt6 = (Button) findViewById(R.id.backToBeforeBt6);
		backToBeforeBt6.setOnClickListener(socl);
		seeLoadPicBt = (Button) findViewById(R.id.seeLoadPic);
		seeDisPicBt = (Button) findViewById(R.id.seeDisPic);
		confirmResultBt = (Button) findViewById(R.id.confirmResult);
		seeLoadPicBt.setOnClickListener(socl);
		seeDisPicBt.setOnClickListener(socl);
		confirmResultBt.setOnClickListener(socl);
		loadingPortTv = (TextView) findViewById(R.id.loadingPort);
		DischargePortTv = (TextView) findViewById(R.id.dischargePort);
		loadingAmountTv = (TextView) findViewById(R.id.loadingAmount);
		DischargeAmountTv = (TextView) findViewById(R.id.dischargeAmount);
		lastUnAmountTv = (TextView) findViewById(R.id.lastUnAmountTv);
		// 设置参数到textview
		loadingPortTv.setText(getInStringValue("getLoadingPort"));
		DischargePortTv.setText(getInStringValue("getDischargePort"));
		loadingAmountTv.setText(getInStringValue("getLoadingCount")
				+ getInStringValue("getGoodsUnit"));
		DischargeAmountTv.setText(getInStringValue("getDischarge_Count")
				+ getInStringValue("getGoodsUnit"));

		if (!getInStringValue("getLoadingCount").equals(
				getInStringValue("getDischarge_Count"))) {
			loadingAmountTv.setTextColor(getResources().getColor(
					R.color.already_con));
			DischargeAmountTv.setTextColor(getResources().getColor(
					R.color.not_con));
		} else {
			loadingAmountTv.setTextColor(getResources().getColor(
					R.color.wait_con));
			DischargeAmountTv.setTextColor(getResources().getColor(
					R.color.wait_con));
		}
		unitTv = (TextView) findViewById(R.id.unitTv);
		unitTv.setText(getInStringValue("getGoodsUnit"));
		infoAmountEt = (EditText) findViewById(R.id.infoAmountEt);
		infoAmountEt.setInputType(EditorInfo.TYPE_CLASS_PHONE);

		// 如果该签单已经确认，则确认按钮变灰不可操作，输入edittext不可操作
		LL = (LinearLayout) findViewById(R.id.LinearLayout);
		if ("true".equals(getInStringValue("getFinish"))) {
			LL.setVisibility(View.GONE);
			lastUnAmountTv.setText("已确认实卸数为："
					+ getInStringValue("getShipperConfirmAmount")
					+ getInStringValue("getGoodsUnit"));
		} else {
			if ("true".equals(getInStringValue("getShipperConfirm"))) {
				lastUnAmountTv.setText("上次提交实卸数为："
						+ getInStringValue("getShipperConfirmAmount")
						+ getInStringValue("getGoodsUnit"));
			} else {
				lastUnAmountTv.setText("");
			}
		}
	}

	private final String getInStringValue(String key) {
		try {
			return getIntent().hasExtra(key) == false ? "" : getIntent()
					.getStringExtra(key);
		} catch (Exception e) {
			return "";
		}

	}

	protected final OnClickListener socl = new OnClickListener() {

		@Override
		public void onClick(View arg0) {
			switch (arg0.getId()) {
			case R.id.backToBeforeBt6:
				finish();
				break;
			case R.id.seeLoadPic:
				InitDatasDaoImpl.getPictureOfConfirmOrder(THIS,
						getInStringValue("getId"), HttpConstant.LOAD);
				// 页面跳转载initDatasDaoImpl文件中
				break;
			case R.id.seeDisPic:
				InitDatasDaoImpl.getPictureOfConfirmOrder(THIS,
						getInStringValue("getId"), HttpConstant.DISCHARGE);
				// 页面跳转载initDatasDaoImpl文件中
				break;
			case R.id.confirmResult:
				final String infoCount = infoAmountEt.getText().toString();
				if (infoAmountEt.getText().length() <= 0) {
					FunctionOfActivity.makeToaskShow(THIS, "请输入实卸数");
				} else {
					new AlertDialog.Builder(THIS)
							.setMessage(
									"您输入的实卸数为：" + infoCount
											+ getInStringValue("getGoodsUnit")
											+ "\n是否确认此签单？")
							.setTitle("提示")
							.setIcon(android.R.drawable.ic_dialog_info)
							.setPositiveButton("是",
									new DialogInterface.OnClickListener() {
										@Override
										public void onClick(
												DialogInterface dialog,
												int which) {
											try {
												InitDatasDaoImpl
														.submitConfirmOrder(
																THIS,
																infoCount,
																getInStringValue("getId"));
											} catch (Exception e) {
												FunctionOfActivity
														.makeToaskShow(THIS,
																"请求异常");

											}
										}
									}).setNegativeButton("取消", null).show();
				}
				break;
			default:
				break;
			}
		}

	};

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			finish();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
}
